import { notification, message } from 'antd';
import cloneDeep from 'lodash/cloneDeep';
import _map from 'lodash/map';
import _uniq from 'lodash/uniq';
import _pull from 'lodash/pull';
import { GetDateThree, FormatnumberZero } from '../../../../function';
import {
  getHistory,
  getHot,
  getHistoryByType,
  getNormal,
} from '../../../../services/allposition_service';

export default {
  namespace: 'etfGroup',
  state: {
    history: { code: 0, message: '', list: [] },
    hot: { code: 0, message: '', list: [] },
  },

  effects: {
    *GetHistory({ payload, callback }, { call, put }) {
      const response = yield call(getHistory, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveHistory',
          payload: { code: 1, message: response.message, list: response.data },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *GetHot({ payload, callback }, { call, put }) {
      const response = yield call(getHot, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveHot',
          payload: { code: 1, message: response.message, list: response.data },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *GetNormal({ payload, callback }, { call, put }) {
      const response = yield call(getNormal, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveHot',
          payload: { code: 1, message: response.message, list: response.data },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    /* res.list.push({
                x:(list[j].date||'').split(' ')[0],
                y1:list[j].percent,
                y2:list[j].oneweek,
                y3:list[j].twoweek,
                y4:list[j].onemonth,
                y5:list[j].threemonth,
                y6:list[j].sixmonth,
                y7:list[j].oneyear,
              });
            } */
    *GetHistoryByType({ payload, callback }, { call, put }) {
      const response = yield call(getHistoryByType, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        const res = { codes: response.data.codes, list: [] };
        const { list } = response.data;
        for (let j = 0; j < list.length; j++) {
          const dateJ = (list[j].date || '').split(' ')[0];
          res.list.push({
            type: '组合净值',
            x: dateJ,
            value: list[j].percent,
          });
          res.list.push({
            type: 'SPY净值',
            x: dateJ,
            value: list[j].spyPercent,
          });
          // res.list.push({
          //   type:'相对强度2',
          //   x:dateJ,
          //   value:list[j].percent/list[j].spyPercent +1,
          // });
          res.list.push({
            type: '相对强度',
            x: dateJ,
            value: list[j].relativestrengthline,
          });
          res.list.push({
            type: '20日相对强度',
            x: dateJ,
            value: list[j].relativestrengthline20,
          });
          res.list.push({
            type: '跑赢SPY比率(1周）',
            x: dateJ,
            value: list[j].oneweek,
          });
          res.list.push({
            type: '跑赢SPY比率(2周）',
            x: dateJ,
            value: list[j].twoweek,
          });
          res.list.push({
            type: '跑赢SPY比率(1月）',
            x: dateJ,
            value: list[j].onemonth,
          });
          res.list.push({
            type: '跑赢SPY比率(3月）',
            x: dateJ,
            value: list[j].threemonth,
          });
          res.list.push({
            type: '跑赢SPY比率(6月）',
            x: dateJ,
            value: list[j].sixmonth,
          });
          res.list.push({
            type: '跑赢SPY比率(1年）',
            x: dateJ,
            value: list[j].oneyear,
          });
        }
        if (callback) callback(res);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
  },

  reducers: {
    saveHistory(state, action) {
      return {
        ...state,
        history: action.payload,
      };
    },
    saveHot(state, action) {
      return {
        ...state,
        hot: action.payload,
      };
    },
  },
};
